package com.mk.service.platform;

import act.apidoc.Description;
import act.controller.Controller;
import act.validation.NotBlank;
import com.mk.domain.CpSsorole;
import com.mk.enums.ErrorCode;
import com.mk.exception.UserException;
import com.mk.util.S2;
import org.osgl.mvc.annotation.*;
import org.osgl.util.S;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Description("平台角色维护")
@Controller("cpSsorole")
@SuppressWarnings("unused")
public class CpSsoroleService extends BasePlatformService {

    @Inject
    private CpSsorole.Mapper cpSsoroleMapper;

    @GetAction("list.json")
    @With(ILogin.class)
    @Description("平台-角色-获取全部有效角色列表（已实现）")
    public List<CpSsorole> listSsorolePlatform() {
        List<CpSsorole> list = cpSsoroleMapper.selectAll();
        Map<String, CpSsorole> map = new HashMap<>();
        List<CpSsorole> result = new ArrayList<>();
        list.forEach(dto -> {
            findSon(dto, list);
            if (S.isBlank(dto.getRolepid())) {
                result.add(dto);
            }
        });
        return result;
    }

    /**
     * 递归的将所有子类都赋予父类的属性
     */
    private void findSon(CpSsorole dto, List<CpSsorole> list) {
        List<CpSsorole> sublist = list.stream().filter(
            d -> S.eq(d.getRolepid(), (dto.getRoleid()))
        ).collect(Collectors.toList());
        if (!sublist.isEmpty()) {
            sublist.forEach(subdto -> findSon(subdto, list));
            dto.setList(sublist);
        }
    }

    @GetAction("{roleid}/roleplat.json")
    @Description("查询平台角色")
    @With(ILogin.class)
    public CpSsorole roleplat(@Description("角色id") String roleid) {
        return cpSsoroleMapper.selectByRoleid(roleid);
    }

    @PostAction("add.json")
    @With(ILogin.class)
    @Description("平台-角色-新增平台角色,返回新增的角色主键（已实现）")
    public String addSsorolePlatform(
        @Description("平台角色对象")
            CpSsorole cpSsorolePlatform
    ) {
        String uuid = S2.UUID();
        cpSsorolePlatform.setRoleid(uuid);
        UserException.throwIf(S.isBlank(cpSsorolePlatform.getRolecode()), ErrorCode.E200000);
        cpSsoroleMapper.insertTemplate(cpSsorolePlatform);
        return uuid;
    }

    @PutAction("update.json")
    @With(ILogin.class)
    @Description("平台-角色-修改平台角色,返回角色主键（已实现）")
    public String updateSsorolePlatform(
        @Description("平台角色对象")
            CpSsorole cpSsorole
    ) {
        cpSsoroleMapper.updateTemplateById(cpSsorole);
        return cpSsorole.getRoleid();
    }

    @DeleteAction("{roleid}/del.json")
    @With(ILogin.class)
    @Description("平台-角色-删除平台角色(已实现)")
    public int deleteSsorolePlatform(
        @Description("平台角色主键")
            String roleid
    ) {
        return cpSsoroleMapper.deleteRole(roleid);
    }


    @PostAction("saveSsoroleResource.json")
    @With(ILogin.class)
    @Description("平台-角色-分配资源给平台角色(已实现)")
    public int saveSsoroleResource(
        @Description("角色id")
        @NotBlank String roleid,
        @Description("平台资源id列表") List<String> resourceidList
    ) {
        return cpSsoroleMapper.saveRoleResource(roleid, resourceidList);
    }

    @PostAction("saveRoleStructure.json")
    @With(ILogin.class)
    @Description("平台-角色-保存平台角色结构(已实现)")
    public int saveRoleStructure(
        @Description("平台角色资源对象")
            List<CpSsorole> cpSsoroles
    ) {
        return cpSsoroleMapper.saveRoleStructure(cpSsoroles);
    }


}
